﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Configuration;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Xml;
using ND.Tool;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace AutoCode.Win
{
    /// <summary>
    /// 模块编号：ND.Tool.Common
    /// 作用：应用程序开发常用函数
    /// 作者：牛达
    /// 编写日期：2009-04-13
    /// Log编号：1
    /// 修改描述：对此类进行了命名规范
    /// 作者：牛达
    /// 修改日期：2009-04-15
    /// Log编号：2
    /// 修改描述：对此类进行了修缮
    /// 作者：牛达
    /// 修改日期：2012-03-02
    /// </summary>
    public static class Common
    {
        private static Random m_rnd = new Random();

        #region 文件操作

        /// <summary> 
        /// 读取文件
        /// </summary> 
        /// <param name="p_filePath">文件路径</param> 
        /// <returns></returns>
        public static string ReadFile(string p_filePath)
        {
            try
            {
                return MyReadFile(p_filePath, false);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ReadFile]"); }
        }
        private static string MyReadFile(string p_filePath, bool p_hasMapPath)
        {
            if (!p_hasMapPath)
                p_filePath = HttpContext.Current.Server.MapPath(p_filePath);
            string fRes;
            //判断文件是否存在
            if (!File.Exists(p_filePath))
            {
                fRes = "\0";
            }
            else
            {
                FileStream fs = new FileStream(p_filePath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
                fRes = sr.ReadToEnd();
                fs.Close();
            }
            return fRes;
        }

        /// <summary> 
        /// 创建文件
        /// </summary> 
        /// <param name="p_filePath">文件路径</param> 
        /// <param name="p_content">文件内容</param> 
        /// <returns></returns>
        public static string CreateFile(string p_filePath, string p_content)
        {
            try
            {
                return MyCreateFile(p_filePath, p_content, true);
                //return MyCreateFile(p_filePath, p_content, false);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateFile]"); }
        }
        private static string MyCreateFile(string p_filePath, string p_content, bool p_hasMapPath)
        {
            if (!p_hasMapPath)
                p_filePath = HttpContext.Current.Server.MapPath(p_filePath);
            string fRes;
            FileStream fs;
            string dir = Path.GetDirectoryName(p_filePath);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            if (!File.Exists(p_filePath))
            {
                fs = new FileStream(p_filePath, FileMode.OpenOrCreate, FileAccess.Write);
                fRes = "Create";
            }
            else
            {
                fs = new FileStream(p_filePath, FileMode.Truncate, FileAccess.Write);
                fRes = "Edit";
            }
            UTF8Encoding utf8 = new UTF8Encoding(false);
            StreamWriter w = new StreamWriter(fs, utf8);
            w.Write(p_content);
            w.Flush();
            fs.Close();

            return fRes;
        }

        /// <summary>
        /// 保存原始文件
        /// </summary>
        /// <param name="p_buffer"></param>
        /// <param name="p_filePath">文件路径</param>
        /// <returns>文件目录</returns>
        public static void SaveFile(byte[] p_buffer, string p_filePath)
        {
            try
            {
                CreateFldByPath(p_filePath, false);

                MemoryStream ms = new MemoryStream(p_buffer);
                FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(p_filePath), FileMode.Create);

                ms.WriteTo(fs);
                ms.Close();
                fs.Close();
                fs = null;
                ms = null;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.SaveFile]"); }
        }

        /// <summary> 
        /// 删除文件
        /// </summary> 
        /// <param name="p_filePath">文件路径</param> 
        /// <returns></returns>
        public static void DeleteFile(string p_filePath)
        {
            try
            {
                p_filePath = HttpContext.Current.Server.MapPath(p_filePath);
                if (File.Exists(p_filePath))
                {
                    File.Delete(p_filePath);
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.DeleteFile]"); }
        }

        /// <summary> 
        /// 创建文件夹
        /// </summary> 
        /// <param name="p_fldPath">文件夹路径</param> 
        /// <returns></returns>
        public static string CreateFld(string p_fldPath)
        {
            try
            {
                p_fldPath = HttpContext.Current.Server.MapPath(p_fldPath);
                string fRes;
                if (!Directory.Exists(p_fldPath))
                {
                    DirectoryInfo dirinfo = Directory.CreateDirectory(p_fldPath);
                    fRes = "Create";
                }
                else
                {
                    fRes = "Exists";
                }
                return fRes;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateFld]"); }
        }

        /// <summary>
        /// 根据路径创建文件管理目录
        /// </summary>
        /// <param name="p_path"></param>
        /// <param name="p_hasLast"></param>
        /// <returns></returns>
        public static void CreateFldByPath(string p_path, bool p_hasLast)
        {
            try
            {
                string[] arr = p_path.Split(new char[] { '/' });
                string path = "";
                int num = arr.Length;
                if (!p_hasLast)
                    num -= 1;
                for (int i = 0; i < num; i++)
                {
                    if (arr[i] != "")
                    {
                        path += "/" + arr[i];
                        CreateFld(path);
                    }
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateFldByPath]"); }
        }

        /// <summary> 
        /// 删除文件夹
        /// </summary> 
        /// <param name="p_fldPath">文件夹路径</param> 
        /// <returns></returns>
        public static void DeleteFld(string p_fldPath)
        {
            try
            {
                p_fldPath = HttpContext.Current.Server.MapPath(p_fldPath);
                if (Directory.Exists(p_fldPath))
                {
                    Directory.Delete(p_fldPath);
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.DeleteFld]"); }
        }

        /// <summary>
        /// 遍历文件夹中的文件夹
        /// </summary>
        /// <param name="p_fldPath">文件夹路径</param>
        /// <returns></returns>
        public static string[] FindFld(string p_fldPath)
        {
            try
            {
                string[] arrFld = null;
                DirectoryInfo dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(p_fldPath));

                int i = 0, num = dir.GetDirectories().Length;
                arrFld = new string[num];
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    arrFld[i] = d.ToString();
                    i += 1;
                }

                return arrFld;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.FindFld]"); }
        }

        /// <summary>
        /// 遍历文件夹中的文件
        /// </summary>
        /// <param name="p_fldPath">文件夹路径</param>
        /// <returns></returns>
        public static string[] FindFile(string p_fldPath)
        {
            try
            {
                string[] arrFiles = null;
                DirectoryInfo dir = new DirectoryInfo(HttpContext.Current.Server.MapPath(p_fldPath));

                int i = 0, num = dir.GetFiles("*.*").Length;
                arrFiles = new string[num];
                foreach (FileInfo f in dir.GetFiles("*.*"))
                {
                    arrFiles[i] = f.ToString();
                    i += 1;
                }

                return arrFiles;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.FindFile]"); }
        }

        /// <summary>
        /// 从图片地址下载图片到本地磁盘
        /// </summary>
        /// <param name="p_fldPath">本地文件夹</param>
        /// <param name="p_url"></param>
        public static void DownloadImage(string p_fldPath, string p_url, string p_fileName)
        {
            try
            {
                WebClient mywebclient = new WebClient();
                string url = p_url;
                string newfilename = p_fileName;
                string suffixName = GetSuffixName(p_url); //文件后缀
                string filepath = HttpContext.Current.Server.MapPath(p_fldPath) + newfilename + suffixName;
                mywebclient.DownloadFile(url, filepath);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.DownloadImage]"); }
        }
        public static void DownloadImage2(string p_fileUrl, string p_filePath)
        {
            try
            {
                WebClient webClient = new WebClient();
                string filePath = HttpContext.Current.Server.MapPath(p_filePath);
                webClient.DownloadFile(p_fileUrl, filePath);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.DownloadImage2]"); }
        }

        /// <summary>
        /// 遍历替换文件内容
        /// </summary>
        /// <param name="p_fldPath">完整物理文件夹路径</param>
        /// <param name="oldValue"></param>
        /// <param name="p_newValue"></param>
        public static void ReplaceAllFileByGetDirectories(string p_fldPath, string p_suffixName, string p_oldValue, string p_newValue)
        {
            try
            {
                string[] fileNames = Directory.GetFiles(p_fldPath);
                string[] directories = Directory.GetDirectories(p_fldPath);
                foreach (string file in fileNames)
                {
                    if (GetSuffixName(file) == p_suffixName)
                    {
                        string cont = MyReadFile(file, true);
                        cont = cont.Replace(p_oldValue, p_newValue);
                        MyCreateFile(file, cont, true);
                    }
                }
                foreach (string dir in directories)
                {
                    ReplaceAllFileByGetDirectories(dir, p_suffixName, p_oldValue, p_newValue);
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ReplaceAllFileByGetDirectories]"); }
        }

        /// <summary>
        /// 获取文件后缀名
        /// </summary>
        /// <param name="p_filePath">文件路径</param>
        /// <returns></returns>
        public static string GetSuffixName(string p_filePath)
        {
            try
            {
                return p_filePath.Substring(p_filePath.LastIndexOf("."));
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetSuffixName]"); }
        }

        /// <summary>
        /// 获取文件名
        /// </summary>
        /// <param name="p_filePath">文件路径</param>
        /// <returns></returns>
        public static string GetFileName(string p_filePath)
        {
            try
            {
                return p_filePath.Substring(p_filePath.LastIndexOf("/"));
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetFileName]"); }
        }

        /// <summary>
        /// 导入指定xml文件
        /// </summary>
        /// <param name="p_xmlPath">xml路径</param>
        /// <returns></returns>
        public static XmlDocument LoadXml(string p_xmlPath)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(HttpContext.Current.Server.MapPath(p_xmlPath));
            return xml;
        }

        #endregion

        #region 常用函数

        /// <summary>
        /// 确保用户输入的是安全的
        /// </summary>
        /// <param name="p_text">用户输入的字符串</param>
        /// <param name="p_maxLength">用户输入最大字数限制</param>
        /// <returns></returns>
        public static string InputText(string p_text, int p_maxLength)
        {
            try
            {
                p_text = p_text.Trim();
                if (string.IsNullOrEmpty(p_text))
                    return string.Empty;
                if (p_text.Length > p_maxLength)
                    p_text = p_text.Substring(0, p_maxLength);
                p_text = Regex.Replace(p_text, "[\\s]{2,}", " ");	//two or more spaces
                p_text = Regex.Replace(p_text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");	//<br>
                p_text = Regex.Replace(p_text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");	//&nbsp;
                p_text = Regex.Replace(p_text, "<(.|\\n)*?>", string.Empty);	//any other tags
                p_text = p_text.Replace("'", "''");
                return p_text;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.InputText]"); }
        }

        /// <summary>
        /// 清除没用的字符（任何非单词字符）
        /// </summary>
        /// <param name="p_text">用户输入的字符串</param>
        /// <returns></returns>
        public static string CleanNonWord(string p_text)
        {
            try
            {
                return Regex.Replace(p_text, "\\W", "");
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CleanNonWord]"); }
        }

        /// <summary>
        /// 设置安全的分页码
        /// </summary>
        /// <param name="p_page"></param>
        /// <returns></returns>
        public static string SetSecurityPage(string p_page)
        {
            try
            {
                if (!String.IsNullOrEmpty(p_page))
                {
                    if (!IsNumber(p_page))
                        p_page = "1";
                }
                else
                    p_page = "1";
                return p_page;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.SetSecurityPage]"); }
        }

        /// <summary>
        /// 判断信息是否来自外部
        /// </summary>
        /// <returns></returns>
        public static bool ChkPost()
        {
            try
            {
                string server_v1, server_v2;
                bool chkpost = false;
                server_v1 = HttpContext.Current.Request.ServerVariables["Http_Referer"];
                server_v2 = HttpContext.Current.Request.ServerVariables["Server_Name"];
                int len = server_v2.Length;
                if (null != server_v1 && server_v1 != "")
                {
                    string YY = server_v1.Substring(7, len);
                    if (YY != server_v2)
                        chkpost = false;
                    else
                        chkpost = true;
                }
                else { chkpost = false; }
                return chkpost;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ChkPost]"); }
        }

        /// <summary> 
        /// 截取适当长度的文字串
        /// </summary> 
        /// <param name="p_str">字符串</param> 
        /// <param name="p_strLen">长度</param> 
        /// <param name="p_ifEnd">是否加省略号</param> 
        /// <returns></returns> 
        public static string CutStr(string p_str, int p_strLen, bool p_ifEnd)
        {
            try
            {
                p_str = StripHtml(p_str);
                int l;
                l = p_str.Length;
                if (l > p_strLen)
                {
                    if (p_ifEnd == true)
                        return p_str.Substring(0, p_strLen) + "...";
                    else
                        return p_str.Substring(0, p_strLen);
                }
                else
                    return p_str;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CutStr]"); }
        }

        /// <summary>
        /// 去掉Html标签
        /// </summary>
        /// <param name="p_strHtml">内容</param>
        /// <returns></returns>
        public static string StripHtml(string p_strHtml)
        {
            try
            {
                Regex objRegExp = new Regex("(<[a-zA-Z].*?>)|(<[/][a-zA-Z].*?>)|\\s?");
                string strOutput = objRegExp.Replace(p_strHtml, "");
                strOutput = strOutput.Replace("<", "&lt;");
                strOutput = strOutput.Replace(">", "&gt;");
                return strOutput;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.StripHtml]"); }
        }

        /// <summary> 
        /// 得到年月日时间YYYY-MM-DD HH:mm:ss格式
        /// </summary> 
        /// <returns></returns>
        public static string GetTimeId()
        {
            try
            {
                string YMDHMS = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                string mSec = GetmSec();
                return YMDHMS + mSec;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetTimeID]"); }
        }

        /// <summary> 
        /// 得到毫秒
        /// </summary> 
        /// <returns></returns>
        public static string GetmSec()
        {
            try
            {
                string mSec = System.DateTime.Now.Millisecond.ToString();
                mSec = "00" + mSec;
                return mSec.Substring(mSec.Length - 3, 3);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetmSec]"); }
        }

        /// <summary> 
        /// 得到客户端IP
        /// </summary> 
        /// <returns></returns> 
        public static string GetClientIP()
        {
            try
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetClientIP]"); }
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="p_str">字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ToEncryptPassword(string p_str)
        {
            try
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(p_str, "SHA1");
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ToEncryptPassword]"); }
        }

        #region 字符串判断
        /// <summary>
        /// 判断是否为数字(true代表不是数字，false代表是数字)
        /// </summary>
        /// <param name="p_s">字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string p_s)
        {
            try
            {
                int flag = 0;
                char[] str = p_s.ToCharArray();
                for (int i = 0; i < str.Length; i++)
                {
                    if (Char.IsNumber(str[i]))
                    {
                        flag++;
                    }
                    else
                    {
                        flag = -1;
                        break;
                    }
                }
                if (flag > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.IsNumber]"); }
        }

        public static bool IsInt(string s)
        {
            int result;
            return Int32.TryParse(s, out result);
        }

        public static bool IsDouble(string s)
        {
            double result;
            return double.TryParse(s, out result);
        }

        public static bool IsDecimal(string s)
        {
            decimal result;
            return decimal.TryParse(s, out result);
        }
        #endregion

        /// <summary>
        /// 删除字符串尾部特殊字符
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="endStr">尾部特殊字符</param>
        /// <returns></returns>
        public static string CutEndStr(string p_str, string p_endStr)
        {
            try
            {
                int strLen = p_str.Length, endStrLen = p_endStr.Length;
                string endStrIn = p_str.Substring(strLen - endStrLen);
                string returnStr = "";
                if (endStrIn == p_endStr)
                    returnStr = p_str.Substring(0, strLen - endStrLen);
                else
                    returnStr = p_str;
                return returnStr;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CutEndStr]"); }
        }

        /// <summary>
        /// 获得网页源码
        /// </summary>
        /// <param name="p_sourceUrl">网页Url</param>
        /// <returns></returns>
        public static string GetHtmlCont(string p_sourceUrl)
        {
            try
            {
                WebRequest request;
                WebResponse response;
                Stream resStream;
                StreamReader sr;
                request = WebRequest.Create(p_sourceUrl);
                response = request.GetResponse();
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream, System.Text.Encoding.Default);
                return sr.ReadToEnd();
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetHtmlCont]"); }
        }
        public static string GetHtmlCont(string p_sourceUrl, string p_referer, int p_encoding)
        {
            try
            {
                HttpWebRequest request;
                HttpWebResponse response;
                Stream resStream;
                StreamReader sr;
                request = (HttpWebRequest)HttpWebRequest.Create(p_sourceUrl);
                CookieContainer cookieCon = new CookieContainer();
                request.CookieContainer = cookieCon;
                request.Referer = p_referer; //设置HTTP_REFERER
                response = (HttpWebResponse)request.GetResponse();
                resStream = response.GetResponseStream();
                Encoding ed = null;
                switch (p_encoding)
                {
                    case 0:
                        ed = System.Text.Encoding.Default;
                        break;
                    case 1:
                        ed = System.Text.Encoding.UTF8;
                        break;
                    default:
                        ed = System.Text.Encoding.Default;
                        break;
                }
                sr = new StreamReader(resStream, ed);
                return sr.ReadToEnd();
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetHtmlCont]"); }
        }

        /// <summary>
        /// 获得网页里符合特定条件的内容
        /// </summary>
        /// <param name="p_htmlCont">网页源码</param>
        /// <param name="p_regular">正则表达式</param>
        /// <returns></returns>
        public static string[] GetSpecificCont(string p_htmlCont, string p_regular)
        {
            try
            {
                MatchCollection mc = Regex.Matches(p_htmlCont, p_regular, RegexOptions.IgnoreCase);
                int num = mc.Count, i = 0;
                string[] arrSpecificCont = new string[num];
                foreach (Match m in mc)
                {
                    arrSpecificCont[i] += m.Groups[1];
                    i += 1;
                }
                return arrSpecificCont;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetSpecificCont]"); }
        }

        /// <summary>
        /// 正则表达式进行忽略大小写的字符串替换
        /// </summary>
        /// <param name="p_cont"></param>
        /// <param name="p_searchWord"></param>
        /// <returns></returns>
        public static string RegexReplace(string p_cont, string p_searchWord)
        {
            try
            {
                string pain = p_cont;//字符串
                System.Text.RegularExpressions.MatchCollection m = Regex.Matches(pain, p_searchWord, RegexOptions.IgnoreCase);//忽略大小写搜索字符串中的要害字 
                for (int j = 0; j < m.Count; j++)
                {
                    //j×31为插入html标签使pain字符串增加的长度，注重下面两句不能交换位置，否则将出现HTML标签插入错误。 
                    pain = pain.Insert((m[j].Index + p_searchWord.Length + j * 31), "</span>");//要害字后插入html标签
                    pain = pain.Insert((m[j].Index + j * 31), "<span class=\"searchword\">");//要害字前插入html标签
                }

                return pain;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.RegexReplace]"); }
        }

        /// <summary>
        /// 正则表达式进行忽略大小写的字符串替换2
        /// </summary>
        /// <param name="p_cont"></param>
        /// <param name="p_old"></param>
        /// <param name="p_new"></param>
        /// <returns></returns>
        public static string RegexReplace2(string p_cont, string p_old, string p_new)
        {
            try
            {
                return System.Text.RegularExpressions.Regex.Replace(p_cont, p_old, p_new);
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.RegexReplace2]"); }
        }

        /// <summary>
        /// 选择性列表初始状态（DropDownList）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p_drp"></param>
        /// <param name="p_value"></param>
        public static void SelectList<T>(DropDownList p_drp, T p_value)
        {
            try
            {
                for (int i = 0; i < p_drp.Items.Count; i++)
                {
                    if (p_drp.Items[i].Selected == true)
                    {
                        p_drp.Items[i].Selected = false;
                    }
                }
                if (typeof(T) == typeof(string))
                {
                    for (int i = 0; i < p_drp.Items.Count; i++)
                    {
                        if (p_drp.Items[i].Value == Convert.ToString(p_value))
                        {
                            p_drp.Items[i].Selected = true;
                        }
                    }
                }
                else if (typeof(T) == typeof(bool))
                {
                    for (int i = 0; i < p_drp.Items.Count; i++)
                    {
                        if (Convert.ToBoolean(p_drp.Items[i].Value) == Convert.ToBoolean(p_value))
                        {
                            p_drp.Items[i].Selected = true;
                        }
                    }
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.SelectList]"); }
        }

        /// <summary>
        /// 选择性列表初始状态（RadioButtonList）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p_radl"></param>
        /// <param name="p_value"></param>
        public static void SelectList<T>(RadioButtonList p_radl, T p_value)
        {
            try
            {
                for (int i = 0; i < p_radl.Items.Count; i++)
                {
                    if (p_radl.Items[i].Selected == true)
                    {
                        p_radl.Items[i].Selected = false;
                    }
                }
                if (typeof(T) == typeof(string))
                {
                    for (int i = 0; i < p_radl.Items.Count; i++)
                    {
                        if (p_radl.Items[i].Value == Convert.ToString(p_value))
                        {
                            p_radl.Items[i].Selected = true;
                        }
                    }
                }
                else if (typeof(T) == typeof(bool))
                {
                    for (int i = 0; i < p_radl.Items.Count; i++)
                    {
                        if (Convert.ToBoolean(p_radl.Items[i].Value) == Convert.ToBoolean(p_value))
                        {
                            p_radl.Items[i].Selected = true;
                        }
                    }
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.SelectList]"); }
        }

        /// <summary>
        /// 标题中所含搜索关键词设置为特定颜色
        /// </summary>
        /// <param name="p_title"></param>
        /// <param name="p_pattern"></param>
        /// <param name="p_class"></param>
        /// <returns></returns>
        public static string SetRedSearchWord(string p_title, string p_pattern,string p_class)
        {
            try
            {
                string res = "";
                if (null != p_pattern && p_pattern != "")
                {
                    res = Regex.Replace(p_title, @"(" + p_pattern + ")", "<span class=\"" + p_class + "\">$1</span>", RegexOptions.IgnoreCase);
                }
                else
                {
                    res = p_title;
                }
                return res;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.SetRedSearchWord]"); }
        }

        /// <summary>
        /// 得到随机字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string getRandomString(int length)
        {
            try
            {
                StringBuilder sb = new StringBuilder(length);
                for (int i = 0; i < length; i++)
                {
                    sb.Append(getRandomChar());
                }
                return sb.ToString();
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.getRandomString]"); }
        }
        public static char getRandomChar()
        {
            try
            {
                int ret = m_rnd.Next(122);
                while (ret < 48 || (ret > 57 && ret < 65) || (ret > 90 && ret < 97))
                {
                    ret = m_rnd.Next(122);
                }
                return (char)ret;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.getRandomChar]"); }
        }

        /// <summary>
        /// 根据值显示相应字串
        /// </summary>
        /// <param name="p_word"></param>
        /// <param name="arrValue"></param>
        /// <param name="arrResult"></param>
        /// <returns></returns>
        public static string FormatWord(string p_word, string[] arrValue, string[] arrResult)
        {
            try
            {
                string result = "";
                for (int i = 0; i < arrValue.Length; i++)
                {
                    if (p_word == arrValue[i])
                        result = arrResult[i];
                }
                return result;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.FormatWord]"); }
        }
        #endregion

        #region 分页组件

        /// <summary>
        /// 显示分页代码
        /// </summary>
        /// <param name="p_url">传递查询参数</param>
        /// <param name="p_totleNum">总条数</param>
        /// <param name="p_numPerPage">每页条数</param>
        /// <param name="p_page">当前页数</param>
        /// <param name="p_showJump">是否显示跳转按钮</param>
        /// <param name="p_showInfo">分页形式</param>
        /// <param name="p_pagestyle"></param>
        /// <returns></returns>
        public static string ShowPage(string p_url, int p_totleNum, int p_numPerPage, int p_page, bool p_showJump, bool p_showInfo, int p_pagestyle, string p_target)
        {
            try
            {
                string strTemp = "";
                int n, p, uming_i, ii;
                if (p_totleNum <= p_numPerPage)
                {
                    strTemp = "";
                }
                else
                {
                    p_url = p_url.Trim();
                    if (p_url != "") { p_url = p_url + "&"; }
                    if (p_page < 1) { p_page = 1; }
                    if (p_totleNum % p_numPerPage == 0)
                    {
                        n = p_totleNum / p_numPerPage;
                    }
                    else
                    {
                        n = p_totleNum / p_numPerPage + 1;
                    }

                    //显示跳转按钮所需脚本
                    if (p_showJump)
                    {
                        strTemp = "<script language=\"JavaScript\">" +
                            "<!--" + Environment.NewLine + "function JumpURL(){" +
                            "var inputpage=document.getElementById(\"inputpage\").value;" +
                            "var JURL=document.getElementById(\"JURL\").value;" +
                            "location.href=\"?\" + JURL + \"page=\" + inputpage;" +
                            "}//--></script>";
                    }

                    strTemp += "<table align=\"center\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"page_table\">" +
                        "<tr><td>";

                    //显示分页信息
                    if (p_showInfo)
                    {
                        strTemp = strTemp + "页次：" + p_page + "/" + n + "页 ";
                        strTemp = strTemp + p_numPerPage + "条/页 ";
                        strTemp = strTemp + "共" + p_totleNum + "条 &nbsp;&nbsp;";
                    }

                    //显示特定分页类型
                    switch (p_pagestyle)
                    {
                        case 0:
                            if (p_page < 2)
                            {
                                strTemp = strTemp + "<span class=\"page_disabled\">首页 上页 </span>";
                            }
                            else
                            {
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=1\">首页</a> ";
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=" + (p_page - 1) + "\">上页</a> ";
                            }
                            if (n - p_page < 1)
                            {
                                strTemp = strTemp + "<span class=\"page_disabled\">下页 尾页 </span>";
                            }
                            else
                            {
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=" + (p_page + 1) + "\">下页</a> ";
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=" + n + "\">尾页</a>  ";
                            }
                            break;
                        case 1:
                            p = (p_page - 1) / 10;
                            if (p * 10 > 0)
                            {
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=" + p * 10 + "\" title=上十页>|&lt;&lt;</a>   ";
                            }
                            uming_i = 1;
                            for (ii = p * 10 + 1; ii <= p * 10 + 10; ii++)
                            {
                                if (Convert.ToInt32(ii) == p_page)
                                {
                                    strTemp = strTemp + "<font class=\"page_disabled\"><strong>" + Convert.ToInt32(ii) + "</strong></font> ";
                                }
                                else
                                {
                                    strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" +
                                        p_url + "page=" + ii + "\"><strong>" + Convert.ToInt32(ii) + "</strong></a> ";
                                }
                                if (ii == n) { break; }
                                uming_i = uming_i + 1;
                            }
                            if (ii <= n && uming_i == 11)
                            {
                                strTemp = strTemp + "<a target=\"" + p_target + "\" href=\"?" + p_url + "page=" + ii + "\" title=下十页>&gt;&gt;|</a>  ";
                            }
                            break;
                        default:
                            break;
                    }

                    //显示跳转按钮
                    if (p_showJump)
                    {
                        strTemp += "&nbsp;<input type=\"text\" class=\"txt\" size=\"3\" id=\"inputpage\" name=\"inputpage\" />" +
                            "<input type=\"hidden\" id=\"JURL\" value=\"" + p_url + "\" />" +
                            "<input type=\"button\" class=\"but\" value=\"GO\" onclick=\"JumpURL();\" /> ";
                    }

                    strTemp = strTemp + "</td></tr></table>";
                }
                return strTemp;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ShowPage]"); }
        }

        /// <summary>
        /// 显示分页代码(静态版本)
        /// </summary>
        /// <param name="p_url">传递查询参数</param>
        /// <param name="p_totleNum">总条数</param>
        /// <param name="p_numPerPage">每页条数</param>
        /// <param name="p_page">当前页数</param>
        /// <param name="p_htmlSuffix">静态后缀类型</param>
        /// <param name="p_showInfo">分页形式</param>
        /// <param name="p_pagestyle"></param>
        /// <param name="p_target"></param>
        /// <returns></returns>
        public static string ShowHtmlPage(string p_url, int p_totleNum, int p_numPerPage, int p_page, string p_htmlSuffix, bool p_showInfo, int p_pagestyle, string p_target)
        {
            try
            {
                if (string.IsNullOrEmpty(p_htmlSuffix))
                    p_htmlSuffix = "shtml";
                string strTemp = "";
                int n;
                if (p_totleNum <= p_numPerPage)
                {
                    strTemp = "";
                }
                else
                {
                    p_url = p_url.Trim();
                    if (p_url != "") { p_url = p_url + ""; }
                    if (p_page < 1) { p_page = 1; }
                    if (p_totleNum % p_numPerPage == 0)
                    {
                        n = p_totleNum / p_numPerPage;
                    }
                    else
                    {
                        n = p_totleNum / p_numPerPage + 1;
                    }

                    strTemp += "<table align=\"center\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"nd_table\">" +
                        "<tr><td class=\"nd_td\">";

                    //显示分页信息
                    if (p_showInfo)
                    {
                        strTemp = strTemp + "页次：" + p_page + "/" + n + "页 ";
                        strTemp = strTemp + p_numPerPage + "条/页 ";
                        strTemp = strTemp + "共" + p_totleNum + "条 &nbsp;&nbsp;";
                    }

                    //显示特定分页类型
                    switch (p_pagestyle)
                    {
                        case 3:
                            if (p_page > 1)
                            {
                                strTemp = strTemp + "<a target=\"" + p_target + "\" class=\"nd_f1\" href=\"" + p_url + (p_page - 1) + "." + p_htmlSuffix + "\">" +
                                    "上一页</a>&nbsp;&nbsp;";
                            }

                            if (n > 10)
                            {
                                if (p_page < 7)
                                {
                                    for (int k = 1; k < 10; k++)
                                    {
                                        if (k == p_page)
                                        {
                                            strTemp = strTemp + "<span class=\"nd_f3\">" + k + "</span>&nbsp;&nbsp;";
                                        }
                                        else
                                        {
                                            strTemp = strTemp + "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + k + "." + p_htmlSuffix + "\">" +
                                                "" + k + "</a>&nbsp;&nbsp;";
                                        }
                                    }
                                    strTemp = strTemp + "...&nbsp;&nbsp;";
                                }
                                else if (p_page > n - 6)
                                {
                                    strTemp += "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + "1." + p_htmlSuffix + "\">1</a>" +
                                        "&nbsp;&nbsp;" + "...&nbsp;&nbsp;";
                                    for (int k = n - 8; k <= n; k++)
                                    {
                                        if (k == p_page)
                                        {
                                            strTemp = strTemp + "<span class=\"nd_f3\">" + k + "</span>&nbsp;&nbsp;";
                                        }
                                        else
                                        {
                                            strTemp = strTemp + "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + k + "." + p_htmlSuffix + "\">" +
                                                "" + k + "</a>&nbsp;&nbsp;";
                                        }
                                    }
                                }
                                else
                                {
                                    strTemp += "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + "1." + p_htmlSuffix + "\"><strong>1</strong></a>" +
                                        "&nbsp;&nbsp;" + "...&nbsp;&nbsp;";
                                    for (int k = p_page - 4; k <= p_page + 4; k++)
                                    {
                                        if (k == p_page)
                                        {
                                            strTemp = strTemp + "<span class=\"nd_f3\">" + k + "</span>&nbsp;&nbsp;";
                                        }
                                        else
                                        {
                                            strTemp = strTemp + "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + k + "." + p_htmlSuffix + "\">" +
                                                "" + k + "</a>&nbsp;&nbsp;";
                                        }
                                    }
                                    strTemp = strTemp + "...&nbsp;&nbsp;";
                                }
                            }
                            else
                            {
                                for (int k = 1; k <= n; k++)
                                {
                                    if (k == p_page)
                                    {
                                        strTemp = strTemp + "<span class=\"nd_f3\">" + k + "</span>&nbsp;&nbsp;";
                                    }
                                    else
                                    {
                                        strTemp += "<a target=\"" + p_target + "\" class=\"nd_f2\" href=\"" + p_url + k + "." + p_htmlSuffix + "\">" + k + "</a>" +
                                            "&nbsp;&nbsp;";
                                    }
                                }
                            }
                            if (n - p_page >= 1)
                            {
                                strTemp += "<a target=\"" + p_target + "\" class=\"nd_f1\" href=\"" + p_url + (p_page + 1) + "." + p_htmlSuffix + "\">" +
                                    "下一页</a>";
                            }
                            break;
                        default:
                            break;
                    }

                    strTemp = strTemp + "</td></tr></table>";
                }
                return strTemp;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.ShowHtmlPage]"); }
        }

        #endregion

        #region 数据自定义操作

        /// <summary>
        /// 信息列表分页（存储过程型01）
        /// </summary>
        /// <param name="p_db">数据库连接</param>
        /// <param name="p_tableName">要显示的表或多个表的连接</param>
        /// <param name="p_selectWhat">要显示的字段列表</param>
        /// <param name="p_pageSize">每页显示的记录个数</param>
        /// <param name="p_page">要显示那一页的记录</param>
        /// <param name="p_where">查询条件,不需where</param>
        /// <param name="p_order">排序</param>
        /// <param name="p_id">自动编号</param>
        /// <param name="p_dist">是否添加查询字段的 DISTINCT 默认0不添加/1添加</param>
        /// <param name="p_pageCount">查询结果分页后的总页数</param>
        /// <param name="p_counts">查询到的记录数</param>
        /// <returns></returns>
        public static DataSet GetList_SQL01(DataBase p_db, string p_tableName, string p_selectWhat, int p_pageSize, int p_page, string p_where, string p_order, bool p_sort, string p_id, out int p_pageCount, out int p_counts, bool p_dist)
        {
            try
            {
                DbParameter pageCount = p_db.AddOutParameter("@pageCount", 0);
                DbParameter counts = p_db.AddOutParameter("@Counts", 0);
                p_db.AddParameter("@tblName", p_tableName);
                p_db.AddParameter("@fldName", p_selectWhat);
                p_db.AddParameter("@pageSize", p_pageSize);
                p_db.AddParameter("@page", p_page);
                p_db.AddParameter("@fldSort", p_order);
                p_db.AddParameter("@Sort", p_sort);
                p_db.AddParameter("@strCondition", p_where);
                p_db.AddParameter("@ID", p_id);
                p_db.AddParameter("@Dist", p_dist);
                using (DataSet ds = p_db.ExecuteDataSet("nd_get_list_01"))
                {
                    p_db.ClearParameters();
                    p_pageCount = Convert.ToInt32(pageCount.Value);
                    p_counts = Convert.ToInt32(counts.Value);
                    return ds;
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetList_SQL01]"); }
        }

        /// <summary>
        /// 信息列表分页（存储过程型02）
        /// </summary>
        /// <param name="p_db">数据库连接</param>
        /// <param name="p_tableName">要显示的表或多个表的连接</param>
        /// <param name="p_selectWhat">要显示的字段列表</param>
        /// <param name="p_pageSize">每页显示的记录个数</param>
        /// <param name="p_page">要显示那一页的记录</param>
        /// <param name="p_where">查询条件,不需where</param>
        /// <param name="p_order">排序</param>
        /// <param name="p_id">自动编号</param>
        /// <param name="p_pageCount">查询结果分页后的总页数</param>
        /// <param name="p_counts">查询到的记录数</param>
        /// <returns></returns>
        public static DataSet GetList_SQL02(DataBase p_db, string p_tableName, string p_selectWhat, int p_pageSize, int p_page, string p_where, string p_order, string p_id, out int p_pageCount, out int p_counts)
        {
            try
            {
                DbParameter recordCount = p_db.AddOutParameter("@RecordCount", 0);
                p_db.AddParameter("@PSize", p_pageSize);
                p_db.AddParameter("@PageNow", p_page);
                p_db.AddParameter("@SelectWhat", p_selectWhat);
                p_db.AddParameter("@Where", p_where);
                p_db.AddParameter("@Sort", p_order);
                p_db.AddParameter("@TableName", p_tableName);
                p_db.AddParameter("@FldName", p_id);
                DataSet ds = p_db.ExecuteDataSet("nd_get_list_02");
                p_counts = Convert.ToInt32(recordCount.Value);
                if (p_counts % p_pageSize == 0)
                {
                    p_pageCount = p_counts / p_pageSize;
                }
                else
                {
                    p_pageCount = p_counts / p_pageSize + 1;
                }
                return ds;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetList_SQL02]"); }
        }

        /// <summary>
        /// 信息列表分页（Access型）
        /// </summary>
        /// <param name="p_db">数据库连接</param>
        /// <param name="p_tableName">要显示的表或多个表的连接</param>
        /// <param name="p_selectWhat">要显示的字段列表</param>
        /// <param name="p_pageSize">每页显示的记录个数</param>
        /// <param name="p_page">要显示那一页的记录</param>
        /// <param name="p_where">查询条件,不需where</param>
        /// <param name="p_order">排序</param>
        /// <param name="p_id">自动编号</param>
        /// <param name="p_pageCount">查询结果分页后的总页数</param>
        /// <param name="p_counts">查询到的记录数</param>
        /// <returns></returns>
        public static DataSet GetList_Access(DataBase p_db, string p_tableName, string p_selectWhat, int p_pageSize, int p_page, string p_where, string p_order, string p_id, out int p_pageCount, out int p_counts)
        {
            try
            {
                int startCount;
                string sql = "";

                p_counts = Convert.ToInt32(p_db.ExecuteScalar("SELECT count(0) FROM " + p_tableName + " WHERE 1=1 " + p_where));
                if (p_counts % p_pageSize == 0)
                {
                    p_pageCount = p_counts / p_pageSize;
                }
                else
                {
                    p_pageCount = p_counts / p_pageSize + 1;
                }
                if (p_page < 1)
                {
                    startCount = p_page * p_pageSize;
                }
                else
                {
                    startCount = (p_page - 1) * p_pageSize;
                }
                if (p_page <= 1)
                {
                    sql = "SELECT TOP " + p_pageSize + " " + p_selectWhat + " FROM " + p_tableName + " WHERE 1=1 " + p_where + " " + p_order;
                }
                else
                {
                    sql = "SELECT TOP " + p_pageSize + " " + p_selectWhat + " FROM " + p_tableName + " WHERE (" + p_id + " NOT IN (SELECT TOP " + startCount + " " + p_id + " FROM " + p_tableName + " WHERE 1=1 " + p_where + " " + p_order + ")) " + p_where + " " + p_order;
                }
                DataSet ds = p_db.ExecuteDataSet(sql);
                return ds;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.GetList_Access]"); }
        }

        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p_drp"></param>
        /// <param name="p_list"></param>
        /// <param name="p_textField"></param>
        /// <param name="p_valueField"></param>
        /// <param name="p_selectInfo"></param>
        public static void BindingDropDownList<T>(DropDownList p_drp, IList<T> p_list, string p_textField, string p_valueField, string p_note)
        {
            try
            {
                p_drp.Items.Add(new ListItem(p_note, ""));
                p_drp.DataSource = p_list;
                p_drp.DataTextField = p_textField;
                p_drp.DataValueField = p_valueField;
                p_drp.DataBind();
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.BindingDropDownList]"); }
        }

        #endregion

        #region 图片处理
        /// <summary>
        /// 创建缩略图
        /// </summary>
        /// <param name="p_filePath">原图片目录</param>
        /// <param name="p_thumbnailFolder">缩略图保存目录</param>
        /// <param name="p_width">指定的最大宽度</param>
        /// <param name="p_height">指定的最大高度</param>
        public static string CreateThumbnail(string p_filePath, string p_thumbnailFolder, int p_width, int p_height)
        {
            try
            {
                string filePath = HttpContext.Current.Server.MapPath(p_filePath);
                string thumbnailFilePath = "";
                if (File.Exists(filePath))
                {
                    Graphics g = null;
                    string thumbnailFileName = GetFileName(p_filePath).Replace(".", "_" + p_width + "X" + p_height + ".");

                    CreateFldByPath(p_thumbnailFolder, true);
                    thumbnailFilePath = HttpContext.Current.Server.MapPath(p_thumbnailFolder + thumbnailFileName);

                    System.Drawing.Image initImage = System.Drawing.Image.FromFile(filePath);
                    if (initImage.Width <= p_width && initImage.Height <= p_height)
                    {
                        initImage.Save(thumbnailFilePath);
                    }
                    else
                    {
                        //缩略图宽、高计算
                        double newWidth = initImage.Width;
                        double newHeight = initImage.Height;

                        //宽大于高或宽等于高（横图或正方）
                        if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
                        {
                            //如果宽大于模版
                            if (initImage.Width > p_width)
                            {
                                //宽按模版，高按比例缩放
                                newWidth = p_width;
                                newHeight = initImage.Height * p_width / initImage.Width;
                            }
                        }
                        //高大于宽（竖图）
                        else
                        {
                            //如果高大于模版
                            if (initImage.Height > p_height)
                            {
                                //高按模版，宽按比例缩放
                                newHeight = p_height;
                                newWidth = initImage.Width * p_height / initImage.Height;
                            }
                        }

                        System.Drawing.Image newImage = new Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
                        g = Graphics.FromImage(newImage);
                        g.DrawImage(initImage, new Rectangle(0, 0, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight)), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);
                        g.Dispose();

                        newImage.Save(thumbnailFilePath);
                    }
                }
                else { throw new Exception("原文件不存在！"); }

                return thumbnailFilePath;
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateThumbnail]"); }
        }

        /// <summary>
        /// 图片水印
        /// </summary>
        /// <param name="p_filePath">原图片目录</param>
        /// <returns></returns>
        private static void CreateWatermarkPic(string p_filePath, string p_waterMarkPic)
        {
            try
            {
                using (System.Drawing.Image watermark = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(p_waterMarkPic)))
                {
                    System.Drawing.Image initImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(p_filePath));
                    System.Drawing.Image newImage = new Bitmap(initImage.Width, initImage.Height);
                    Graphics g = Graphics.FromImage(newImage);
                    g.DrawImage(initImage, new Rectangle(0, 0, initImage.Width, initImage.Height), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);

                    g.DrawImage(watermark, initImage.Width - watermark.Width - 10, initImage.Height - watermark.Height - 10, watermark.Width, watermark.Height);

                    g.Dispose();

                    newImage.Save(p_filePath);
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateWatermarkPic]"); }
        }

        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="p_filePath">原图片目录</param>
        /// <returns></returns>
        private static void CreateWatermarkWord(string p_filePath, string p_waterMarkWord)
        {
            try
            {
                using (System.Drawing.Image initImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(p_filePath)))
                {
                    System.Drawing.Image newImage = new Bitmap(initImage.Width, initImage.Height);
                    Graphics g = Graphics.FromImage(newImage);
                    g.DrawImage(initImage, new Rectangle(0, 0, initImage.Width, initImage.Height), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);

                    Font fontWater = new Font("黑体", 10);
                    Brush brushWater = new SolidBrush(Color.Black);
                    g.DrawString(p_waterMarkWord, fontWater, brushWater, 10, initImage.Height - 30);

                    g.Dispose();

                    newImage.Save(p_filePath);
                }
            }
            catch (Exception exc) { throw new Exception(exc.Message + "[ND.Tool.Common.CreateWatermarkWord]"); }
        }
        #endregion
    }
}